import { Application } from 'egg';
import { DataTypes, Model } from 'sequelize';

export interface DishAttributes {
  id?: number;
  restaurant_id: number;
  name: string;
  picture: string;
  created_at?: Date;
  updated_at?: Date;
}

export interface DishInstance extends Model<DishAttributes>, DishAttributes {}

export default (app: Application) => {
  const { STRING, INTEGER, DATE } = DataTypes;

  const Dish = app.model.define<DishInstance>('Dish', {
    id: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    restaurant_id: {
      type: INTEGER,
      allowNull: false,
      comment: '美食店ID',
    },
    name: {
      type: STRING(100),
      allowNull: false,
      comment: '菜品名称',
    },
    picture: {
      type: STRING(500),
      allowNull: true,
      comment: '菜品图片',
    },
    created_at: {
      type: DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
    },
    updated_at: {
      type: DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
    },
  }, {
    tableName: 'dishes',
    comment: '推荐菜品表',
  });

  // 关联关系
  (Dish as any).associate = () => {
    app.model.Dish.belongsTo(app.model.Restaurant, { foreignKey: 'restaurant_id', as: 'restaurant' });
  };

  return Dish;
};